package com.ac.app.web.action.invest;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import com.ac.app.dm.PickInvest;
import com.ac.app.web.form.PickInvestSearchForm;
import com.ac.app.web.view.PickInvestView;
import com.caac.core.Global;
import com.caac.core.config.OrderClazz;
import com.caac.core.config.QueryClass;
import com.caac.core.dao.CriteriaCallback;
import com.caac.core.dm.OrderProp;
import com.caac.core.util.Page;
import com.caac.core.web.action.BaseExportAction;
import com.caac.core.web.action.ViewForward;
import com.caac.core.web.form.BaseForm;

public class ExportPickInvestAction extends BaseExportAction {
	private static final String QUERY_NAME = "pkInvestQuery";
    private static final String ORDER_NAME = "pkInvestOrder";
    private static final String VIEW_NAME = "pickInvestView";
	
	protected ViewForward doViewExecute(ActionMapping mapping, BaseForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		PickInvestSearchForm myForm = (PickInvestSearchForm) form;
		QueryClass qc = getQuery(QUERY_NAME);
        String whereStr="1=1 ";       
        String key  = request.getParameter("key");
        if(key!=null && key.length()>0 ){
        	String[] keys = key.split(" ");
        	for(int i=0;i<keys.length;i++){
        	   whereStr += " and content like '%"+ keys[i]+"%' ";
        	}
        	whereStr = "("+whereStr+")";
        }
        CriteriaCallback cc = getCc(whereStr);
        if(myForm.getOrderstr()==null || "".equals(myForm.getOrderstr())){
        	myForm.setOrderFlag(1);
        	myForm.setOrderstr("crtTime");
        }        
        OrderClazz oc =  getOc(myForm.getOrderstr(),myForm.getOrderFlag());

        List dmList = getBaseManager().getObjects(PickInvest.class, qc,
				myForm, myForm.isAnd(),oc, cc);

		List<PickInvestView> vList = new ArrayList<PickInvestView>();
		for (int i = 0; i < dmList.size(); i++) {
			PickInvest dm = (PickInvest) dmList.get(i);
			PickInvestView dmView = new PickInvestView();

			dmView.setPickInvest(dm);
			vList.add(dmView);
		}
		
		return new ViewForward(forward(mapping), vList,
				getSelViewProps(VIEW_NAME), null, null);
		
	}
	private static CriteriaCallback getCc(final String whereStr) {
		return new CriteriaCallback() {
			public void execute(Criteria criteria) {
				criteria.add(Expression.sql(whereStr));
			}
		};
	}
	
    private OrderClazz getOc(String orderStr,int orderFlag){
    	List list = new ArrayList();   
    	if(orderStr!=null && !orderStr.equals("")){
    		OrderProp op = new OrderProp();
    		op.setPropName(orderStr);
    		op.setGroupName("pkRecordOrderBk0");
    		if(orderFlag==1){
        		op.setAsc(false);
    		}else{
        		op.setAsc(true);
    		}
    		list.add(op);
    	}
    	return new OrderClazz(list);
    }
}
